package com.example.capsule_chat0322.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.ChatGroupFriendBean;
import com.example.capsule_chat0322.configuration.Constant;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.global.LRUCache;
import com.example.capsule_chat0322.interfaces.ChatGroupFriendItemOnClickInterface;
import com.example.capsule_chat0322.request.UserInfoRequest;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 21:21
 */
public class ChatGroupFriendAdapter extends RecyclerView.Adapter<ChatGroupFriendAdapter.ViewHold> implements View.OnClickListener {

    private List<ChatGroupFriendBean> chatGroupFriendBeanList;
    private ChatGroupFriendItemOnClickInterface chatGroupFriendItemOnClickInterface;
    private RecyclerView rvParent;

    public void setFunctionItemOnClickInterface(ChatGroupFriendItemOnClickInterface chatGroupFriendItemOnClickInterface) {
        this.chatGroupFriendItemOnClickInterface = chatGroupFriendItemOnClickInterface;
    }

    static class ViewHold extends RecyclerView.ViewHolder {

        LinearLayout chatGroupFriendItem;
        TextView chat_group_friend_info;
        ImageView chat_group_friend_avatar;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            chatGroupFriendItem = itemView.findViewById(R.id.chat_group_friend_item);
            chat_group_friend_info = itemView.findViewById(R.id.chat_group_friend_info);
            chat_group_friend_avatar = itemView.findViewById(R.id.chat_group_friend_avatar);
        }
    }


    public ChatGroupFriendAdapter(List<ChatGroupFriendBean> chatGroupFriendBeanList) {
        this.chatGroupFriendBeanList = chatGroupFriendBeanList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_group_friend_item, parent, false);
        view.setOnClickListener(this);
        return new ChatGroupFriendAdapter.ViewHold(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {

        ChatGroupFriendBean chatGroupFriendBean = chatGroupFriendBeanList.get(position);
        holder.chatGroupFriendItem.setVisibility(View.VISIBLE);
        String str = "ID: " + chatGroupFriendBean.getSenderId() + "\n"
                + "昵称: " + chatGroupFriendBean.getNickname();
        holder.chat_group_friend_info.setText(str);
        String avatarPath = chatGroupFriendBean.getAvatarUrl();
        if (avatarPath.contains("/")) {
            LRUCache lruCache = (LRUCache) GlobalAssistMap.getObject("lruCache");
            String cacheKey = avatarPath.substring(avatarPath.lastIndexOf("/") + 1);
            final Bitmap[] bitmap = {(Bitmap) lruCache.get(cacheKey)};
            if (bitmap[0] == null) {
                bitmap[0] = BitmapFactory.decodeFile(cacheKey);
                if (bitmap[0] == null) {
                    UserInfoRequest.getAvatar(new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(@NonNull Message message) {
                            bitmap[0] = BitmapFactory.decodeFile(Constant.IMAGE_PATH + avatarPath.substring(avatarPath.lastIndexOf("/") + 1));
                            lruCache.put(cacheKey, bitmap[0]);
                            holder.chat_group_friend_avatar.setImageBitmap(bitmap[0]);
                            return true;
                        }
                    }), chatGroupFriendBean.getAvatarServerPath());
                } else {
                    holder.chat_group_friend_avatar.setImageBitmap(bitmap[0]);
                }
            } else {
                holder.chat_group_friend_avatar.setImageBitmap(bitmap[0]);
            }
        } else {
            holder.chat_group_friend_avatar.setImageResource(Integer.parseInt(avatarPath));
        }

    }

    @Override
    public int getItemCount() {
        return chatGroupFriendBeanList.size();
    }


    @Override
    public void onClick(View view) {
        //根据RecyclerView获得当前View的位置
        int position = rvParent.getChildAdapterPosition(view);
        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (chatGroupFriendItemOnClickInterface != null) {
            chatGroupFriendItemOnClickInterface.onItemClick(rvParent, view, chatGroupFriendBeanList.get(position).getSenderId());
        }
    }


}
